package com.waters321.rock.core.config.druidmonitor;

import java.util.List;
import java.util.Map;
import com.alibaba.druid.pool.DruidDataSourceStatLogger;
import com.alibaba.druid.pool.DruidDataSourceStatLoggerAdapter;
import com.alibaba.druid.pool.DruidDataSourceStatValue;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DruidMonitorDataPersist extends DruidDataSourceStatLoggerAdapter
    implements DruidDataSourceStatLogger {

  private static DruidStatManagerFacade statManagerFacade = DruidStatManagerFacade.getInstance();

  @Override
  public void log(DruidDataSourceStatValue statValue) {
    // 取的时候是不是应该进里面然后加锁取啊，这样容易丢一些数据 TODO
    // 首页信息
    Map<String, Object> basicStat = statManagerFacade.returnJSONBasicStat();
    // 数据源信息
    List<Map<String, Object>> dataSourceStatDataList =
        statManagerFacade.getDataSourceStatDataList();
    // sql列表信息
    List<Map<String, Object>> sqlList = statManagerFacade.getSqlStatDataList(null);
    // 防火墙拦截信息
    Map<String, Object> wallStatMap = statManagerFacade.getWallStatMap(null);
    // web应用信息
    List<Map<String, Object>> webAppStatData = WebAppStatManager.getInstance().getWebAppStatData();
    // weburi信息
    List<Map<String, Object>> uriStatData = WebAppStatManager.getInstance().getURIStatData();
    // websession信息
    Map<String, Object> sessionStat = WebAppStatManager.getInstance().getSessionStat(null);
    // spring信息
    List<Map<String, Object>> methodStatData = SpringStatManager.getInstance().getMethodStatData();

    // 进行固化 TODO:
    if (basicStat != null) {
      log.info(basicStat.toString());
    }
    if (dataSourceStatDataList != null) {
      log.info(dataSourceStatDataList.toString());
    }
    if (sqlList != null) {
      log.info(sqlList.toString());
    }
    if (wallStatMap != null) {
      log.info(wallStatMap.toString());
    }
    if (webAppStatData != null) {
      log.info(webAppStatData.toString());
    }
    if (uriStatData != null) {
      log.info(uriStatData.toString());
    }
    if (sessionStat != null) {
      log.info(sessionStat.toString());
    }
    if (methodStatData != null) {
      log.info(methodStatData.toString());
    }


    log.info("持久化监控数据成功");
  }



}
